<!--日考勤列表，根据班组筛选的人们某日的出勤时间-->
<template>
  <div class="list-container">
    <el-form class="search-panel" v-model="searchForm" :inline="true">
      <el-form-item label="考勤日期：" prop="date">
        <el-date-picker
          v-model="searchForm.date"
          type="date"
          placeholder="选择考勤日期"
          value-format="yyyy-MM-dd"
        >
        </el-date-picker>
      </el-form-item>
      <el-form-item label-width="90px" class="search-input" label="输入搜索：">
        <el-input v-model="searchForm.name" placeholder="劳务公司名/班组名/人员姓名"></el-input>
      </el-form-item>
      <!-- <el-form-item label-width="90px" label="出勤时间：">
        <el-select v-model="sercchData.time" placeholder="请选择">
          <el-option
            v-for="item in timeOptions"
            :key="item"
            :label="item"
            :value="item"
          >
          </el-option>
        </el-select>
      </el-form-item> -->
      <el-form-item label="考勤状态：">
        <el-select v-model="sercchData.status" placeholder="请选择">
          <el-option
            v-for="item in statusOptions"
            :key="item.value"
            :label="item.label"
            :value="item.value"
          >
          </el-option>
        </el-select>
      </el-form-item>
      <el-form-item>
        <el-button type="primary" @click="onSubmit">查询</el-button>
      </el-form-item>
    </el-form>
    <div class="table-panel">
      <el-row class="table-operate">
        <!-- <el-button
          type="primary"
          icon="el-icon-upload2"
          @click="generateDaySalary"
        >
          生成日工资
        </el-button> -->
        <el-button
          type="primary"
          icon="el-icon-download"
          @click="importDayAttendance"
        >
          导入日考勤
        </el-button>
        <el-button type="primary" icon="el-icon-plus" @click="addDayAttendance">
          新增日考勤
        </el-button>
      </el-row>
      <el-row>
        <el-col :span="5" style="border-right:1px solid #e8e8e8">
          <el-tree
            :data="treeData"
            :props="defaultProps"
            @node-click="handleNodeClick"
          ></el-tree>
        </el-col>
        <el-col :span="19" style="padding-left: 10px">
          <el-table :data="tableData" style="width: 100%">
            <el-table-column
              align="center"
              type="index"
              width="55"
              label="序号"
            >
            </el-table-column>
            <el-table-column align="center" prop="personName" label="姓名">
            </el-table-column>
            <el-table-column align="center" label="出勤时间" prop="time">
              <!-- <template slot-scope="scope">
                <div>
                  {{ scope.row.startTime.substr(10, 16) }}至{{
                    scope.row.endTime.substr(10, 16)
                  }}
                </div>
              </template> -->
            </el-table-column>
            <el-table-column align="center" label="合计">
              <template slot-scope="scope">
                <div v-if="scope.row.totalTime!=null">{{ scope.row.totalTime }}小时</div>
              </template>
            </el-table-column>
            <el-table-column align="center" prop="mode" label="结算">
              <template slot-scope="scope">
                <div v-if="scope.row.mode == 1">打卡机获取</div>
                <div v-else-if="scope.row.mode == 2">人工导入</div>
                <div v-else-if="scope.row.mode == 3">人工+打卡机获取</div>
              </template>
            </el-table-column>
            <el-table-column align="center" prop="status" label="状态">
              <template slot-scope="scope">
                <div v-if="scope.row.isconfirm == 0">未确认</div>
                <div v-else>已确认</div>
                <!-- <div v-else-if="scope.row.isconfirm == 2">班组长已确认</div>
                <div v-else-if="scope.row.isconfirm == 3">异议</div> -->
              </template>
            </el-table-column>
            <el-table-column align="center" label="操作时间" min-width="100">
              <template slot-scope="scope">
                <div>
                  {{
                    scope.row.updateTime == null ? "——" : scope.row.updateTime
                  }}
                </div>
              </template>
            </el-table-column>
            <el-table-column fixed="right" label="操作" align="center" min-width="140">
              <template slot-scope="scope" >
                <el-button
                :disabled="scope.row.isconfirm==1"
                  @click.native.prevent="checkRow(scope.row, scope.$index)"
                  type="text"
                  icon="el-icon-edit"
                  >调整</el-button
                >
                <el-button
                :disabled="scope.row.isconfirm==1"
                  @click.native.prevent="confirmRow(scope.row)"
                  type="text"
                  icon="el-icon-circle-check"
                  >确认</el-button
                >
                <!-- <el-button
                :disabled="scope.row.isconfirm==1"
                  @click.native.prevent="deleteRow(scope.row)"
                  type="text"
                  icon="el-icon-delete"
                  >删除</el-button
                > -->
              </template>
            </el-table-column>
          </el-table>
        </el-col>
      </el-row>
      <el-row class="page">
        <div class="page-tag">
          共{{ Math.ceil(totalData / searchForm.pageSize) }}页/{{
            totalData
          }}条数据
        </div>
        <el-pagination
          background
          @size-change="handleSizeChange"
          @current-change="handleCurrentChange"
          :current-page.sync="searchForm.currentPage"
          :page-sizes="[10, 20, 50]"
          :page-size="searchForm.pageSize"
          layout="sizes, prev, pager, next, jumper"
          :total="totalData"
        >
          >
        </el-pagination>
      </el-row>
    </div>
    <!-- 调整弹窗 -->
    <el-dialog
      class="small-dialog"
      title="调整考勤"
      :visible.sync="dialogFormVisible"
      width="600px"
    >
      <el-form :model="dialogForm" :inline="true" :rules="dialogFormRules" style="text-align: center;">
        <el-form-item label-width="120px" label="考勤开始时间：" prop="startTime">
          <el-time-picker
            v-model="dialogForm.startTime"
            type="datetime"
            placeholder="选择开始时间"
            value-format="HH:mm:ss"
          >
          </el-time-picker>
        </el-form-item>
        <el-form-item label-width="120px" label="考勤结束时间：" prop="endTime">
          <el-time-picker
            v-model="dialogForm.endTime"
            type="datetime"
            placeholder="选择结束时间"
            value-format="HH:mm:ss"
          >
          </el-time-picker>
        </el-form-item>
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button @click="dialogFormVisible = false">取 消</el-button>
        <el-button type="primary" @click="confirmDialog">确 定</el-button>
      </div>
    </el-dialog>
  </div>
</template>

<script>
import {
  getTree,
  getPersonByTeamId,
  getPersonByserviceCorporationId,
} from "@/api/worker";
import {
  getDailyAttendanceList,
  updateAttendance,
  deleteDayAttendances,
  confirmAttendance
} from "@/api/attendance.js";
import store from '../../../../store'
export default {
  data() {
    return {
      sercchData: {
        // name: null,
        status: -1,
        // time: null,
      },
      searchForm: {
        currentPage: 1,
        pageSize: 10,
        date: null,
        name: null,
        status: null,
        teamId: null,
        serviceCorporationId: null,
        projectId:localStorage.getItem('projectId'),
      },
      // timeOptions: ["全部", "2020/11/25", "2020/11/24", "2020/11/23"],
      statusOptions: [
        { value: -1, label: "全部" },
        { value: 0, label: "未确认" },
        { value: 1, label: "已确认" },
      ],
      // currentPage: 1,
      // pageSize: 10,
      totalData: 0,
      treeData: [],
      flag: 1, //1--一级劳务公司，0--二级班组
      defaultProps: {
        children: "children",
        label: "name",
      },
      tableData: [],
      dialogFormVisible: false,
      dialogForm: {
        startTime: "09:00",
        endTime: "18:00",
        // mode: 0,
        // count: 1,
      },
      dialogFormRules: {
        startTime: [
          { required: true, message: "请选择考勤开始时间", trigger: "blur" },
        ],
        endTime: [
          { required: true, message: "请选择考勤结束时间", trigger: "blur" },
        ],
      },
      // modeOptions: [
      //   { value: 0, label: "按天" },
      //   { value: 1, label: "按小时" },
      // ],
      index: null,
    };
  },
  created() {
    // console.log(this.$route.query.date)
    this.searchForm.date = this.$route.query.date;
    this.getTreeData();
    this.getTableData();
  },
  methods: {
    // 获取劳务公司、班组树形数据
    getTreeData() {
      getTree().then((res) => {
        // console.log(res)
        this.treeData = res.data.data;
      });
    },
    onSubmit() {
      this.getTableData();
    },
    //   生成日工资
    generateDaySalary() {},
    //   导入日考勤
    importDayAttendance() {
      this.$router.push("/attendance/daily/import");
    },
    //   新增日考勤
    addDayAttendance() {
      this.$router.push("/attendance/daily/add");
    },
    //   页面大小
    handleSizeChange(val) {
      this.searchForm.pageSize = val;
      this.getTableData();
    },
    //更改当前页
    handleCurrentChange(val) {
      this.searchForm.currentPage = val;
      this.getTableData();
    },
    //发送请求获取表格数据
    getTableData() {
      if (this.sercchData.status != -1)
        this.searchForm.status = this.sercchData.status;
        else this.searchForm.status = null;
      if (this.flag === 0) {
        // 根据班组id查询班组人员————选择班组
        getDailyAttendanceList(this.searchForm).then((res) => {
          // console.log(res);
          this.tableData = res.data.data.records;
          this.totalData = res.data.data.total;
          // this.processTableData();
        });
      } else {
        // 根据劳务公司id查询劳务公司人员————选择劳务公司
        getDailyAttendanceList(this.searchForm).then((res) => {
          // console.log(res);
          this.tableData = res.data.data.records;
          this.totalData = res.data.data.total;
          // this.processTableData();
        });
      }
    },
    // 树状图
    handleNodeClick(res) {
      // console.log(res)
      if (res.children == null) {
        // console.log("班组")
        this.flag = 0;
        this.searchForm.teamId = res.id;
        this.searchForm.serviceCorporationId = null;
        this.getTableData();
      } else {
        // console.log("劳务公司")
        this.flag = 1;
        this.searchForm.serviceCorporationId = res.id;
        this.searchForm.teamId = null;
        this.getTableData();
      }
    },
    checkRow(res, index) {
      // console.log(res.time.substring(6,10))
      this.dialogForm = {
        startTime: res.time.substring(0,5)+":00",
        endTime: res.time.substring(6,11)+":00",
        // attendanceMode: 0,
        // count: res.count,
      }
      this.dialogFormVisible = true
      this.index = index;
    },
    confirmRow(res) {
      this.$confirm(
        "是否确认 " + this.searchForm.date + " " + res.personName + "的考勤?",
        "日考勤确认",
        {
          confirmButtonText: "确认",
          cancelButtonText: "取消",
          type: "warning",
        }
      )
        .then(() => {
          confirmAttendance({
            date:this.searchForm.date,
            personId:res.personId
          }).then(e=>{
            this.$message({
            type: "success",
            message: "确认考勤成功!",
          });
          this.getTableData()
          })
          
          // this.$router.push("/attendance/month/detail");
        })
        .catch(() => {
          this.$message({
            type: "info",
            message: "已取消确认",
          });
        });
    },
    deleteRow(res) {
      this.$confirm(
        "是否确认删除" +
          res.personName +
          "的 " +
          this.searchForm.date +
          " 考勤?",
        "日考勤删除",
        {
          confirmButtonText: "确定",
          cancelButtonText: "取消",
          type: "warning",
        }
      )
        .then(() => {
          deleteDayAttendances({
            date: this.searchForm.date,
            personId: res.personId,
          }).then((e) => {
            // console.log(e);
            this.$message({
              type: "success",
              message: "删除成功!",
            });
            this.getTableData();
          });
        })
        .catch(() => {
          this.$message({
            type: "info",
            message: "已取消删除",
          });
        });
      // this.$confirm(
      //   "是否确认删除" +
      //     res.personName +
      //     "的 " +
      //     this.searchForm.date +
      //     " 考勤?",
      //   "日考勤删除",
      //   {
      //     confirmButtonText: "确认",
      //     cancelButtonText: "取消",
      //     type: "warning",
      //   }
      // )
      //   .then(() => {
      //     deleteDayAttendances({
      //       date: this.searchForm.date,
      //       person_id: this.res.personId,
      //     }).then((e) => {
      //       console.log(e);
      //       this.$message({
      //         type: "success",
      //         message: "删除成功!",
      //       });
      //     });
      //   })
      //   .catch(() => {
      //     this.$message({
      //       type: "info",
      //       message: "已取消删除",
      //     });
      //   });
    },
    // 弹窗确认
    confirmDialog() {
      // 修改当前考勤详情
      // const result = {
      //       personId:this.tableData[this.index].personId,
      //       eventEndTime:this.searchForm.date+" "+this.dialogForm.endTime,
      //       eventStartTime:this.searchForm.date+" "+this.dialogForm.startTime,
      //     }
      //     console.log(result)
      updateAttendance({
        personId: this.tableData[this.index].personId,
        eventEndTime: this.searchForm.date + " " + this.dialogForm.endTime,
        eventStartTime: this.searchForm.date + " " + this.dialogForm.startTime,
      }).then((e) => {
        // console.log(e);
        this.getTableData()
      });
      this.dialogFormVisible = false;
    },
  },
};
</script>
<style lang="scss" >
// .updata-dialog {
//   .el-dialog {
//     .el-dialog__body {
//       // text-align: center;
//       .el-form {
//         width: 90%;
//         margin: 0 auto;
//         text-align: center;
//         // margin-left: 110px;
//         .el-form-item__label {
//           width: 120px;
//         }
//         .el-select {
//           width: 220px;
//         }
//         .el-input {
//           width: 220px;
//         }
//       }
//     }
//   }
// }
</style>